#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import multiprocessing
import time


def myprint(obj):
    sys.stdout.write(str(obj) + '\n')


def stage_1(cond):
    """perform first stage of work, then notify stage_2 to continue"""

    name = multiprocessing.current_process().name
    myprint('Starting %s' % name)
    with cond:
        myprint('%s done and ready for stage 2' % name)
        cond.notify_all()


def stage_2(cond):
    """wait for the condition telling us stage_1 is done"""

    name = multiprocessing.current_process().name
    myprint('Starting %s' % name)
    with cond:
        cond.wait()
        myprint('%s running' % name)


if __name__ == '__main__':
    condition = multiprocessing.Condition()
    s1 = multiprocessing.Process(name='s1', target=stage_1,
                                 args=(condition, ))
    s2_clients = [multiprocessing.Process(name='stage_2[%d]' % i,
                  target=stage_2, args=(condition, )) for i in range(1,
                  3)]

    for c in s2_clients:
        c.start()
        time.sleep(1)
    s1.start()

    s1.join()
    for c in s2_clients:
        c.join()
